<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/PersonalManagement' }"
        >个人中心</el-breadcrumb-item
      >
      <el-breadcrumb-item>修改密码</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="原密码" prop="passWord">
        <el-input
          type="password"
          v-model="ruleForm.passWord"
          autocomplete="off"
          class="input_box"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="newpassWord">
        <el-input
          type="password"
          v-model="ruleForm.newpassWord"
          autocomplete="off"
          class="input_box"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
          class="input_box"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="changePassWord">确定</el-button>
        <el-button @click="dialogFormVisible">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>

import md5 from 'js-md5'

export default {
  data() {
    return {
      ruleForm: {
        passWord: "",
        newpassWord: "",
        checkPass: "",
      },
      rules: {
        passWord: [
          { required: true, message: "请输入原密码", trigger: "blur" },
        ],
        newpassWord: [
          { required: true,message:"请输入新密码",trigger: "blur" },
        ],
        checkPass: [
          { required: true,message:"与新密码不一致", trigger: "blur" },
          {validator:(rule, value, callback) =>  {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.newpassWord) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      }}
        ],
      },
    };
  },
  methods: {
    dialogFormVisible(){},
    changePassWord() {
      this.$refs.ruleForm.validate((vaild) => {
        if (vaild) {
          this.$http({
            url: "/admin/changePassWord",
            method: "POST",
            data: {
              passWord: md5(this.ruleForm.passWord),
              newpassWord: md5(this.ruleForm.newpassWord),
            },
          }).then((res) => {
            if (!res.errorMsg) {
              this.dialogFormVisible = false;
              this.$message({
                message: "修改密码成功",
                type: "success",
                duration: 3000,
              });
              setTimeout(() => {
                location.href = "/login";
              }, 3000);
            }
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
.input_box {
  width: 300px;
  height: 37px;
}
</style>